home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 126-150 / disk_136 / bison / getopt.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  2KB  |  67 lines

  1.  
  2. /*LINTLIBRARY*/
  3. #define NULL    0
  4. #define EOF     (-1)
  5. #define ERR(s, c)       if(opterr){\
  6.         extern int strlen(), write();\
  7.         char errbuf[2];\
  8.         errbuf[0] = c; errbuf[1] = '\n';\
  9.         (void) write(2, argv[0], (unsigned)strlen(argv[0]));\
  10.         (void) write(2, s, (unsigned)strlen(s));\
  11.         (void) write(2, errbuf, 2);}
  12.  
  13. extern int strcmp();
  14. extern char *strchr();
  15.  
  16. int     opterr = 1;
  17. int     optind = 1;
  18. int     optopt;
  19. char    *optarg;
  20.  
  21. int
  22. getopt(argc, argv, opts)
  23. int     argc;
  24. char    **argv, *opts;
  25. {
  26.         static int sp = 1;
  27.         register int c;
  28.         register char *cp;
  29.  
  30.         if(sp == 1)
  31.                 if(optind >= argc ||
  32.                    argv[optind][0] != '-' || argv[optind][1] == '\0')
  33.                         return(EOF);
  34.                 else if(strcmp(argv[optind], "--") == NULL) {
  35.                         optind++;
  36.                         return(EOF);
  37.                 }
  38.         optopt = c = argv[optind][sp];
  39.         if(c == ':' || (cp=strchr(opts, c)) == NULL) {
  40.                 ERR(": illegal option -- ", c);
  41.                 if(argv[optind][++sp] == '\0') {
  42.                         optind++;
  43.                         sp = 1;
  44.                 }
  45.                 return('?');
  46.         }
  47.         if(*++cp == ':') {
  48.                 if(argv[optind][sp+1] != '\0')
  49.                         optarg = &argv[optind++][sp+1];
  50.                 else if(++optind >= argc) {
  51.                         ERR(": option requires an argument -- ", c);
  52.                         sp = 1;
  53.                         return('?');
  54.                 } else
  55.                         optarg = argv[optind++];
  56.                 sp = 1;
  57.         } else {
  58.                 if(argv[optind][++sp] == '\0') {
  59.                         sp = 1;
  60.                         optind++;
  61.                 }
  62.                 optarg = NULL;
  63.         }
  64.         return(c);
  65. }
  66.  
  67.